package com.itheima.controller.v1;

import com.itheima.common.R;
import com.itheima.controller.v1.api.CluesControllerApi;
import com.itheima.dto.ClueQueryDto;
import com.itheima.pojo.*;
import com.itheima.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/clues")
@Slf4j
public class CluesController implements CluesControllerApi {
    @Autowired
    private TbActivityService tbActivityService;

    @Autowired
    private TbClueService tbClueService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private TbAssignRecordService tbAssignRecordService;

    @Autowired
    private TbClueTrackRecordService tbClueTrackRecordService;

    @Autowired
    private TbBusinessService tbBusinessService;

    @Value("${clues.path}")
    private String basePath;


    @Override
    @GetMapping("/activity/listselect/{channel}")
    public R listselect(@PathVariable String channel) {
        log.info("获取渠道来源方法");
        return tbActivityService.getByChannel(channel);
    }

    @PostMapping("/clue")
    @Override
    public R add(@RequestBody TbClue tbClue) {
        return tbClueService.add(tbClue);
    }

    @Override
    @GetMapping("/cluePage")
    public R cluePage( ClueQueryDto clueQueryDto) {
        return tbClueService.page(clueQueryDto);
    }

    @Override
    @GetMapping("/deptList")
    public R deptList() {
        return sysDeptService.list();
    }

    @Override
    @GetMapping("/deptUserList/{deptId}")
    public R deptUserList(@PathVariable Long deptId) {
        return tbClueService.userListByDeptId(deptId);
    }

    @Override
    @PostMapping("/addAssignRecordList/{userId}")
    //批量分配线索  添加线索分配记录表tb_assign_record
    public R addAssignRecordList(@PathVariable Long userId,@RequestBody List<Long> clueIds) {
        return tbClueService.updateTransfer(userId,clueIds);
    }

    //单个分配
    @Override
    @PostMapping("/addAssignRecord/{clueOrBusyId}")
    public R addAssignRecord(@PathVariable Long clueOrBusyId,@RequestBody TbAssignRecord tbAssignRecord) {
        return tbAssignRecordService.addAssignRecord(clueOrBusyId,tbAssignRecord);
    }

    //下载模板
    @Override
    @GetMapping("/download")
    public /*R*/void downloadExcel() throws IOException {
        FileInputStream fis=new FileInputStream(basePath);
        ServletOutputStream os = response.getOutputStream();
        byte[]b=new byte[1024];//规格数组
        while (true){
            //每次读取长度1024
            int readLen = fis.read(b);//返回最终读取长度
            if (readLen == -1) {
                break;
            }
            os.write(b,0,readLen);//每次读b长度,从0读到最大长度
        }
        os.close();
        fis.close();
        //return new R(1,"下载批量添加线索模板成功!",null);
    }

    //批量导入线索
    @PostMapping("/upload")
    @Override
    public R uploadExcel(MultipartFile file) {
        return tbClueService.saveSubject(file,tbClueService);
    }

    //根据线索id回显线索详细信息
    @Override
    @GetMapping("/simple/{id}")
    public R clueSimple(@PathVariable Long id) {
        TbClue tbClue = tbClueService.getById(id);
        return new R(1,"回显线索详细信息",tbClue);
    }

    //根据活动id返回活动对象
    @Override
    @GetMapping("/activityById/{activityId}")
    public R activityById(@PathVariable Long activityId) {
        return tbActivityService.getById(activityId);
    }

    //伪线索:更改线索状态
    //status:状态(已分配1  进行中2  回收3  伪线索4)
    //false_count 伪线索失败次数(最大数3次)
    // 大于三次删除
    @Override
    @GetMapping("/status/{id}")
    public R clueStatus(@PathVariable Long id, @RequestParam String status ) {
        return tbClueService.updateStatus(id,status);
    }


    //线索跟进提交1 跟进记录
    @Override
    @PostMapping("/addClueTrackRecord")
    public R addClueTrackRecord(@RequestBody TbClueTrackRecord tbClueTrackRecord) {
        return tbClueTrackRecordService.add(tbClueTrackRecord);
    }

    //线索跟进提交2 更新线索
    @Override
    @PutMapping
    public R updateClue(@RequestBody TbClue tbClue) {

        return tbClueService.updateClueById(tbClue);
    }

    //线索转商机  前端要更改状态码
    //添加进入商机表tb_business
    @PostMapping("/clue_to_business")
    @Override
    public R addTbBusinessClue(@RequestBody TbClue tbClue) {
        //更新线索
        R r1 = tbClueService.updateClueById(tbClue);
        //添加商机
        R r = tbBusinessService.insertClue(tbClue);

        if (r.getCode()==1&&r1.getCode()==1) {
            return R.success("线索转商机成功",null);
        }
        return R.error("线索转商机失败");
    }

    //回显操作记录
    @Override
    @GetMapping("/getClueTrackByClueId/{clueId}")
    public R getClueTrackRecordByClueId(@PathVariable String clueId) {

        return tbClueTrackRecordService.getByClueId(clueId);
    }


}
